<template>
	<div class="wrapper">

		<!--header部分-->
		<header>
			<p>用户管理</p>
		</header>
		<page-rollback></page-rollback>
		<!-- 图片部分 -->
		<div class="user-avatar-wrapper">
			<img :src="user.userImg ? user.userImg : require('../assets/userImg/yhtx01.png')" alt="用户头像">
		</div>
		<!--表单部分-->
		<ul class="form-box">
			<li>
				<div class="title">
					手机号码：
				</div>
				<div class="content">
					{{ originalUser.userId }}
				</div>
			</li>
			<li>
				<div class="title">
					密码：
				</div>
				<div class="content">
					<input type="password" v-model="user.password" placeholder="密码">
				</div>
			</li>
			<li>
				<div class="title">
					用户名称：
				</div>
				<div class="content">
					<input type="text" v-model="user.userName" placeholder="用户名称">
				</div>
			</li>
			<li>
				<div class="title">
					性别：
				</div>
				<div class="content" style="font-size: 3vw;">
					<!--radio表示一组中单选，name相同的为一组，checked 表示默认-->
					<input type="radio" v-model="user.userSex" value="1" style="width:6vw;height: 3.2vw;"> 男
					<input type="radio" v-model="user.userSex" value="0" style="width:6vw;height: 3.2vw;"> 女
				</div>
			</li>
		</ul>
		<div class="button-savechange">
			<button @click="savechange()">保存修改</button>
		</div>
		<div class="button-exitlogin">
			<button @click="exitlogin()">退出登录</button>
		</div>
		<!-- <div class="button-deleteaccount">
			<button @click="deleteAccount()">注销账户</button>
		</div> -->
	</div>
</template>

<script>
import PageRollback from '../components/PageRollback.vue';
	export default {
  components: { PageRollback },
		name: 'AccountManagement',
		data(){
			return{
				user: {},
				originalUser: {} // 用于保存原始用户信息 
			}	  
		},
		created() {
			this.originalUser = this.$getSessionStorage('user');
			this.user = this.originalUser;//初始化表单数据
		},
		methods: {
			isStrongPassword(passwordtest) {
				const uppercaseRegex = /[A-Z]/;
				const lowercaseRegex = /[a-z]/;
				const digitRegex = /[0-9]/;
				const specialCharRegex = /[!@#&*.()]/;

				// 检查密码长度是否大于8位
				const isLengthValid = passwordtest.length > 8;
				return (
					isLengthValid &&
					uppercaseRegex.test(passwordtest) &&
					lowercaseRegex.test(passwordtest) &&
					digitRegex.test(passwordtest) &&
					specialCharRegex.test(passwordtest)
				);
			},
			savechange(){
				console.log("savechange open!")
				if (this.user.password == '') {
					alert('密码不能为空！')
					return;
				}
				// if (!this.isStrongPassword(this.user.password)) {
				// 	alert('密码强度不足！密码应包含至少一个大写字母、一个小写字母、一个数字、一个特殊字符，并且长度大于8位。');
				// 	return;
				// }
				if (this.user.userName == '') {
					alert('用户名不能为空！');
					return;
				}
				let url = `/UserController/updateUser/${this.user.token.token}/${this.user.password}/${this.user.userName}/${this.user.userSex}`
				//更新数据库
				this.$axios.put(url).then(response => {
					if(response.data.result === null || response.data.result === ''){
						alert('Token已过期，请重新登录');
						// 或者触发路由跳转到登录页
						this.$router.push('/login');
					}

					const updatedUser = response.data.result;
					if (updatedUser === null || updatedUser === '') {
						alert('用户不能为空！');
					} else {
						alert('修改成功！');
						this.$setSessionStorage('user', updatedUser);
					}
				})
				.catch(error => {
					console.error(error);
				});
			},
			exitlogin(){
				// 清除会话存储中的用户信息
				window.sessionStorage.clear();
				this.$router.push('/login');
			},
			deleteAccount(){
				this.$axios.post(`UserController/deleteUser/${this.user.token.token}`, this.$qs.stringify({
					// userId: this.user.userId
				}))
					.then(response => {
						if(response.data === null || response.data === ''){
							alert('Token已过期，请重新登录');
							// 或者触发路由跳转到登录页
							this.$router.push('/login');
						}
						if (response.data === 1) {
							alert('注销成功！');
							// 清除会话存储中的用户信息
							window.sessionStorage.clear();
							this.$router.push('/register');
						} else {
							alert('注销失败！');
						}
					})
					.catch(error => {
					console.error(error);
					});
			}			
		}
	}
</script>

<!-- 仅应用到该属性 -->
<style scoped>
	/*********总容器************/
	.wrapper {
		height: 100%;
		width: 100%;
	}

	/*********header部分********/
	.wrapper header {
		width: 100%;
		height: 12vw;

		background-color: #0097FF;
		color: #fff;
		font-size: 4.8vw;

		position: fixed;
		left: 0;
		top: 0;
		/* 给予高层叠级别 */
		z-index: 1000;

		/* 位置上下左右居中 */
		display: flex;
		justify-content: center;
		align-items: center;
	}

	/***********表单部分*************/
	.wrapper .form-box {
		width: 100%;
		margin-top: 12vw;
		/* 利用外边距隔开header部分 */
	}

	.wrapper .form-box li {
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
		display: flex;
		align-items: center;
	}

	.wrapper .form-box li .title {
		/* 表示不可伸展、不可收缩，并且初始大小为视口宽度的 18%，这样，改容器在弹性窗口中固定不变 */
		flex: 0 0 18vw;
		font-size: 3.6vw;
		font-weight: 700;
		color: #666;
	}

	.wrapper .form-box li .content {
		flex: 1;
		/*1代表了伸缩比例*/
	}

	.wrapper .form-box li .content input {
		border: none;
		outline: none;
		width: 100%;
		height: 4vw;
		font-size: 4vw;
	}

	.wrapper .button-savechange {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-exitlogin {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-deleteaccount {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-savechange button {
		width: 100%;
		height: 10vw;

		font-size: 3.8vw;
		font-weight: 700;

		color: #fff;
		background-color: #55aaff;

		border-radius: 4px;

		border: none;
		outline: none;
	}

	.wrapper .button-exitlogin button {
		width: 100%;
		height: 10vw;

		font-size: 3.8vw;
		font-weight: 700;

		color: #fff;
		background-color: #00bbff;

		border-radius: 4px;

		border: none;
		outline: none;
	}

	.wrapper .button-deleteaccount button {
		width: 100%;
		height: 10vw;

		font-size: 3.8vw;
		font-weight: 700;

		color: #fff;
		background-color: #0055ff;

		border-radius: 4px;

		border: none;
		outline: none;
	}

	.user-avatar-wrapper {
		text-align: center;
		padding-top: 20vw;
		/* Adjust the padding as needed */
	}

	.user-avatar {
		width: 30vw;
		/* Adjust the size as needed */
		height: auto;
		max-width: 100%;
	}
</style>

